// internal/planner/logical/plan_nodes.go
package logical

import (
	"NumberBase/internal/planner/types"
	"fmt"
)

// TableScan 表示表扫描操作
type TableScan struct {
	TableName string
	SchemaRef *types.Schema
}

func (s *TableScan) Type() types.PlanType {
	return types.PlanLogicalScan
}

func (s *TableScan) Children() []types.LogicalPlan {
	return nil
}

func (s *TableScan) Schema() *types.Schema {
	return s.SchemaRef
}

func (s *TableScan) String() string {
	return fmt.Sprintf("LogicalTableScan: %s", s.TableName)
}

// Filter 表示过滤操作
type Filter struct {
	Input     types.LogicalPlan
	Condition types.Expression
}

func (f *Filter) Type() types.PlanType {
	return types.PlanLogicalFilter
}

func (f *Filter) Children() []types.LogicalPlan {
	return []types.LogicalPlan{f.Input}
}

func (f *Filter) Schema() *types.Schema {
	return f.Input.Schema()
}

func (f *Filter) String() string {
	return fmt.Sprintf("LogicalFilter: %s\n  %s",
		f.Condition.String(), f.Input.String())
}

// Projection 表示投影操作
type Projection struct {
	Input        types.LogicalPlan
	Expressions  []types.Expression
	OutputSchema *types.Schema
}

func (p *Projection) Type() types.PlanType {
	return types.PlanLogicalProject
}

func (p *Projection) Children() []types.LogicalPlan {
	return []types.LogicalPlan{p.Input}
}

func (p *Projection) Schema() *types.Schema {
	return p.OutputSchema
}

func (p *Projection) String() string {
	// 格式化表达式列表
	var exprList string
	for i, expr := range p.Expressions {
		if i > 0 {
			exprList += ", "
		}
		exprList += expr.String()
	}

	return fmt.Sprintf("LogicalProjection: [%s]\n  %s",
		exprList, p.Input.String())
}
